package engine.utils
{
	import engine.utils.MathUtils;
	
	import fl.motion.Color;
	
	import flash.display.DisplayObject;

	public class ColorUtils
	{
		public static function randomColor(min:int = 0, max:int = 255):uint
		{
			if(min < 0) 		min = 0;
			else if(min > 255) 	min = 255;
			
			if(max < min) 		max = min;
			else if(min > 255) 	min = 255;
			
			var red:uint 	= MathUtils.randomInteger(0, 255);
			var green:uint 	= MathUtils.randomInteger(0, 255);
			var blue:uint 	= MathUtils.randomInteger(0, 255);
			
			return ((red << 16) | (green << 8) | blue);
		}
		
		public static function tint(display:DisplayObject, hex:uint, percent:Number):void
		{
			var color:Color = new Color();
			color.setTint(hex, percent);
			
			display.transform.colorTransform = color;
		}
		
		public static function brighten(hex:uint, multiplier:Number = 1):uint
		{
			if(multiplier > 1) 		multiplier = 1;
			else if(multiplier < 0) multiplier = 0;
			
			var rgb:Vector.<uint>= ColorUtils.toRGB(hex);
			
			rgb[0] += Math.round((255 - rgb[0]) * multiplier);
			rgb[1] += Math.round((255 - rgb[1]) * multiplier);
			rgb[2] += Math.round((255 - rgb[2]) * multiplier);
			
			return ColorUtils.toHex(rgb[0], rgb[1], rgb[2]);
		}
		
		public static function darken(hex:uint, multiplier:Number = 1):uint
		{
			if(multiplier > 1) 		multiplier = 1;
			else if(multiplier < 0) multiplier = 0;
			
			var rgb:Vector.<uint>= ColorUtils.toRGB(hex);
			
			rgb[0] = Math.round(rgb[0] * multiplier);
			rgb[1] = Math.round(rgb[1] * multiplier);
			rgb[2] = Math.round(rgb[2] * multiplier);
			
			return ColorUtils.toHex(rgb[0], rgb[1], rgb[2]);
		}
		
		public static function brightness(hex:uint):uint
		{
			var max:uint = 0;
			var rgb:Vector.<uint> = ColorUtils.toRGB(hex);
			
			if(rgb[0] > max) max = rgb[0];
			if(rgb[1] > max) max = rgb[1];
			if(rgb[2] > max) max = rgb[2];
			
			return max /= 255;
		}
		
		public static function toRGB(hex:uint):Vector.<uint>
		{
			return(new <uint>[(hex >> 16) & 0xFF, (hex >> 8) & 0xFF, hex & 0xFF]);
		}
		
		public static function toHex(red:uint, green:uint, blue:uint):uint
		{
			return(red << 16 | green << 8 | blue);
		}
	}
}